package test.serverframe.armc.server.manager.ribbon;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import test.serverframe.armc.server.manager.service.impl.ServerApiServiceImpl;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author Best
 * @Date 2018/9/20 17:18
 * @ClassName com.example.controller.HelloController
 */
@Api(value = "hello")
@CrossOrigin(origins = "*")
@Validated
@RequestMapping("/hello")
@RestController
public class HelloController {
    @Autowired
    private HelloServie helloServie;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hi")
    @ApiOperation(notes = "hi", value = "hi", httpMethod = "POST")
    public String hi(@RequestParam String name) {
        return helloServie.hiService(name);
    }

    @RequestMapping("/myGetUser")
    @ApiOperation(notes = "myGetUser", value = "myGetUser", httpMethod = "POST")
    public Object getUser(@RequestParam String name, @RequestParam String password) {
        Object ob = restTemplate.getForObject("http://SERVICE-HI/getUser?name=" + name + "&password=" + password, Object.class);
        objectToMap(ob);
        return ob;
    }

    @RequestMapping("/mySetUser")
    @ApiOperation(notes = "mySetUser", value = "mySetUser", httpMethod = "POST")
    public Object setUser(@ApiParam("User") @RequestBody User user) throws IOException {
        String json = "{accout: {id: \'string1\', password: \'string1\', username: \'sadhshas1\'}, age: 10, name: \'string1\'}";
//        URL resource = ServerApiServiceImpl.class.getClassLoader().getResource("interface/list.json");
//        String json = FileUtils.readFileToString(new File(resource.getPath()), "UTF-8");
        Object obj = restTemplate.postForEntity("http://SERVICE-HI/setUser", JSON.parse(json), Object.class);
        Map map = (Map) JSON.toJSON(obj);
        Map map1 = new LinkedHashMap();
        mapBreak(new StringBuilder(""), map, map1);
        System.out.println(map1);
        return obj;
    }

    public static void main(String[] args) throws Exception {
        Object parse = JSON.parse("{accout: {id: 'string1', password: 'string1', username: 'sadhshas1',test:{testid:'123'}}, age: 10, name: 'string1'}");
        Map map = (Map) parse;
        Map map1 = new LinkedHashMap();
        mapBreak(new StringBuilder(""), map, map1);
        System.out.println(map1);
        System.out.println("*****************************************************");
    }

    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }

        Object obj = beanClass.newInstance();

        org.apache.commons.beanutils.BeanUtils.populate(obj, map);

        return obj;
    }

    public static void objectToMap(Object obj) {
//        Map map = (Map) JSON.parse(obj.toString());
        Map map = (Map) JSON.toJSON(obj);
        Map map1 = new LinkedHashMap();
        mapBreak(new StringBuilder(""), map, map1);
        System.out.println(map1);
    }

    /**
     * 将存在嵌套关系的map的key转换为aa.bb.cc的结构
     *
     * @param key
     * @param map
     * @param newMap
     */
    public static void mapBreak(StringBuilder key, Map<String, Object> map, Map<String, Object> newMap) {
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry entry : entries) {
            //key拼接
            key.append(entry.getKey());
            Object value = entry.getValue();
            //判断当前value是否为复杂对象的map
            if (value instanceof Map) {
                key.append(".");
                mapBreak(key, (Map<String, Object>) value, newMap);
                key = new StringBuilder("");
            } else {
                newMap.put(key.toString(), value);
                if (!key.toString().contains(".")) {
                    key = new StringBuilder("");
                } else {
                    String substring = key.substring(0, key.lastIndexOf(".") + 1);
                    key = new StringBuilder(substring);
                }
            }
        }
    }
}
